<template>
  <!-- table表格 -->
  <div class="container">
    <div class="header-content">
      <i class="el-icon-info"></i>
      <span>数据一共{{total }}条</span>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ background: '#f4f4f5' }"
    >
      <el-table-column prop="number" label="试题编号" align="center">
      </el-table-column>
      <el-table-column prop="subject" label="学科" align="center">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" align="center">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" align="center">
        <template #default="{ row }">
          <span>{{
            row.questionType === '1'
              ? '单选'
              : row.questionType === '2'
              ? '多选'
              : '简答'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题干" align="center">
        <template #default="{ row }"
          ><span v-html="row.question"></span
        ></template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" align="center">
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" align="center">
        <template #default="{ row }">
          <span>{{
            row.difficulty === '1'
              ? '简单'
              : row.difficulty === '2'
              ? '一般'
              : '困难'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人" align="center">
      </el-table-column>
      <el-table-column prop="chkState" label="审核状态" align="center">
      </el-table-column>
      <el-table-column prop="chkRemarks" label="审核意见" align="center">
      </el-table-column>
      <el-table-column prop="chkUser" label="审核人" align="center">
      </el-table-column>
      <el-table-column prop="publishState" label="发布状态" align="center">
      </el-table-column>
      <el-table-column label="操作" width="200px">
          <template #default="{ row }">
            <el-button type="text" @click="openDialog('view', row)"
              >预览</el-button
            >
            <el-button
              type="text"
              @click="changeStates('use', row)"
              >审核</el-button
            >
            <el-button type="text"  @click="changeStates('no', row)"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="openDialog('edit', row)"
              :disabled="row.state ? true : false"
              >上架</el-button
            >
            <el-button
              type="text"
              @click="open(row)"
              :disabled="row.state ? true : false"
              >删除</el-button
            >
          </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pagesize"
      :total="total"
      layout="prev,pager, next,sizes, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  list,
  remove,
  detail,
  choiceAdd,
  choice
} from '@/api/hmmm/questions.js'
export default {
  props: {
    tableData: {
      required: true,
      type: Array
    },
    total: Number
  },
  data () {
    return {
      page: 1,
      pagesize: 10
    }
  },
  created () {
    this.tableList()
  },
  methods: {
    async tableList () {
      const { data } = await choice({
        page: this.page,
        pagesize: this.pagesize
      })
      data.items = data.items.map(item => {
        item.addDate = this.$moment(item.addDate).format('YYYY-MM-DD hh:mm:ss')
        return item
      })
      this.tableData = data.items
    },
    // pagesize改变时
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.$emit('initTable')
      this.tableList()
    },
    // page改变时
    handleCurrentChange (val) {
      this.page = val
      this.$emit('initTable')
      this.tableList()
    },
    // 清空input
    clearInput () {
      Object.keys(this.form).forEach(item => {
        this.form[item] = null
      })
      this.initTable()
    },
    // 搜索input
    async searchInput () {
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize,
        ...this.form
      })
      console.log(data)
      this.tableData = data.items
    },
    // 点击新增用户按钮
    async openDialog (row) {
      const { data } = await detail(row)
      console.log(data)
      this.info = data
      this.dialogFormVisible = true
    },
    // 删除 取消确认按钮
    open (id) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发删除请求
          await remove({ id })
          this.initTable()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    }
  }
}
</script>

<style scoped lang="less">
 .header-content {
      background-color: #f4f4f5;
      color: #909399;
      font-size: 13px;
      padding-left: 8px;
      height: 40px;
      line-height: 40px;
      margin-bottom: 20px;
      .el-icon-info {
        margin-right: 5px;
      }
    }
  .el-pagination {
  height: 40px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 20px;
}
</style>
